﻿using System.Text;

namespace _02_C_中string驻留机制以及分析string类型内存占用情况
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "string类型的主流机制";

            //string str1 = "Hello";
            //string str2 = "Hello";
            //string str3 = new string("Hello");

            ////referenceEquals 只比较对象的引用地址，不比较对象的值
            //Console.WriteLine($"str1==str2:{str1 == str2}");//true
            //Console.WriteLine($"$str1和str2的地址是否相同：{object.ReferenceEquals(str1,str2)}");//true

            //Console.WriteLine($"str1==str3:{str1 == str3}");//true
            //Console.WriteLine($"$str1和str2的地址是否相同：{object.ReferenceEquals(str1, str3)}");//false

            //string str4 = "He";
            //string str5 = "llo";
            //string str6 = str4 + str5;
            //Console.WriteLine($"str1==str6：{str1 == str6}");
            //Console.WriteLine($"$str1和str6的地址是否相同：{object.ReferenceEquals(str1, str6)}");//false

            string str = "w";//vs2022好像优化了 用的同一个地址
            str += "o";
            str += "r";
            str += "l";
            str += "d";

            StringBuilder builder=new StringBuilder();
            builder.Append("w");
            builder.Append("o");
            builder.Append("r");
            builder.Append("l");
            builder.Append("d");

            string str1 = builder.ToString();

            Console.ReadKey();
        }
    }
}
